package cn.wolfcode.trip.app.web.controller;

import cn.wolfcode.trip.app.util.UserContext;
import cn.wolfcode.trip.base.domain.Great;
import cn.wolfcode.trip.base.domain.Strategy;
import cn.wolfcode.trip.base.domain.StrategyComment;
import cn.wolfcode.trip.base.domain.StrategyCommentReply;
import cn.wolfcode.trip.base.query.StrategyCommentQueryObject;
import cn.wolfcode.trip.base.query.StrategyCommentReplyQueryObject;
import cn.wolfcode.trip.base.query.StrategyQueryObject;
import cn.wolfcode.trip.base.service.*;
import cn.wolfcode.trip.base.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 攻略点评的接口
 */
@RestController
@RequestMapping("/strategyComments")
public class StrategyCommentController {
    @Autowired
    private IStrategyCommentService strategyCommentService;

    @Autowired
    private IGreatService greatService;

    @Autowired
    private IStrategyCommentReplyService strategyCommentReplyService;

    /**
     * 接口: 获取指定攻略点评id的点评
     * /strategyComments/{id}
     * 动作: GET
     * 参数:
     */
    @GetMapping("/{id}")
    public Object get(@PathVariable Long id){
        StrategyComment strategyComment = strategyCommentService.get(id);
        return strategyComment;
    }


    /**
     * 对某篇攻略点评点赞
     * 接口:/strategyComments/{id}/greats
     * 动作:post
     * 参数:greatType="travel_id"&userId=xxx
     */
    @PostMapping("/{greatTypeId}/greats")
    public Object great(String greatType,@PathVariable Long greatTypeId, Great entity){
        // 定义返回的数据
        Map<String, Object> map = new HashMap<>();
        // 查询是否有该用户的点赞记录
        Great great = greatService.selectGreatByGreatTypeIdAndUserId(greatType,greatTypeId, entity.getUserId());
        if (great != null) {
            // 如果找到了这条记录, 则删除掉这条点赞
            greatService.removeGreatByGreatTypeIdAndUserId(greatType,greatTypeId, entity.getUserId());
        } else {
            // 如果找不到这条记录, 则添加一条点赞数
            greatService.saveGreatByGreatTypeIdAndUserId(greatType,greatTypeId, entity);
        }
        // 重新查询游记点赞数
        Integer greats = greatService.queryForTotalCount(greatType,greatTypeId);
        //设置到攻略点评的点赞数
        strategyCommentService.updateGreatCount(greats,greatTypeId);
        // 设置响应数据
        map.put("greats", greats);
        return map;
    }

    /**
     * 接口: 获取指定攻略点评id的评论
     * /strategyComments/{id}/replies
     * 动作: GET
     * 参数:state=1&pageSize=2&currentPage=x
     */
    @GetMapping("/{strategyCommentId}/replies")
    public Object getReplyByCommentId(StrategyCommentReplyQueryObject qo){
        PageInfo pageInfo = strategyCommentReplyService.query(qo);
        return pageInfo;
    }

    /**
     * 接口: 保存指定攻略点评id的评论
     * /strategyComments/{id}/replies
     * 动作: POST
     * 参数:content=x
     */
    @PostMapping("/{strategyCommentId}/replies")
    public Object saveReply(StrategyCommentReply entity){
        JsonResult jsonResult = new JsonResult();
        //设置评论人
        entity.setReplier(UserContext.getCurrentUser());
        try {
            strategyCommentReplyService.saveReply(entity);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.mark("服务器繁忙, 请稍后再试");
        }
        return jsonResult;
    }

    /**
     * 接口: 获取指定点评id的评论数
     * /strategyComments/{id}/replyCount
     * 动作: GET
     * 参数:
     */
    @GetMapping("/{strategyCommentId}/replyCount")
    public Object getReplyCount(@PathVariable Long strategyCommentId) {
        HashMap<String, Object> map = new HashMap<>();
        int replyCount = strategyCommentReplyService.getCountByStrategyCommentId(strategyCommentId);
        map.put("replyCount",replyCount);
        return map;
    }

}
